<?php
require_once dirname(__FILE__) . '/common.inc.php';
if (isset($_COOKIE[CLIENT])) {
  $_SESSION[CLIENT] = json_decode($_COOKIE[CLIENT]);
}

$is_customised = MiscUtils::getParam('c', 0);
$template = MiscUtils::getParam('t', 'index');
$options = MiscUtils::getParam('opts', json_encode(array()));
$base = MiscUtils::getParam('b', 'base');

$smarty->assign('p', 'ui/default');
$smarty->assign('t', "$template");
$smarty->assign('d', time());
$smarty->assign('opts', urldecode($options));

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[CLIENT]);
  setcookie(CLIENT, '', time() - 1, PATH, DOMAIN);
}
$smarty->assign('u', json_encode((isset($_SESSION[CLIENT])) ? $_SESSION[CLIENT] : array()));

$menuInst = new stdClass();
$menuInst->id = 0;
$menuInst->name = 'ROOT';
$menuInst->link = '';
$menuInst->c = array();
$cond_vals = new stdClass();
$cond_vals->c = 't.appear_in_menu = :v1';
$cond_vals->v = array(':v1' => 1);
_pages(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, 't.order', 'ASC', NULL, NULL, NULL)->d, $menuInst);
$smarty->assign('menuInst', $menuInst);

$menuInst2 = new stdClass();
$menuInst2->id = 0;
$menuInst2->name = 'ROOT';
$menuInst2->link = '';
$menuInst2->c = array();
_pages(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, $menuInst2);

loadPage($template);
$smarty->display("ui/default/$base.tpl");
return;

function loadPage($template) {
  switch ($template) {
    case 'index':
      index();
      break;
    default:
      header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function _pages($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->link = '?i=' . $tmp->id;
      $tmp->c = array();
      $node->c[] = $tmp;
      _pages($arr, $tmp);
    }
  }
}


function _getNav($root, $id) {
  $array = array();
  return _find($root, $id, $array);
}

function _find($haystack, $needle, $array) {
  if ($haystack->id == $needle) {
    return $array;
  }
  foreach ($haystack->c as &$tmp) {
    array_push($array, $tmp);
    $result = _find($tmp, $needle, $array);
    if ($result != NULL) {
      return $result;
    }
    array_pop($array);
  }
  return NULL;
}

function index() {
  global $smarty;
  $smarty->assign('title', '');
  $smarty->assign('desc', '');
  $smarty->assign('keywords', '');
  
  $profiles = DbUtils::get(DbUtils::createPdoInst(), 'pz_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
  $profile = (count($profiles) > 0) ? $profiles[0] : NULL;
  if ($profile) {
    $smarty->assign('title', $profile->title);
    $smarty->assign('desc', $profile->desc);
    $smarty->assign('keywords', $profile->keywords);
  }
  
  $id = MiscUtils::getParam('i', NULL);
  if (!$id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.is_default = :v1';
    $cond_vals->v = array(':v1' => 1);
    $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) == 1) {
      $id = $tmp[0]->id;
    }
  }
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    if (count($tmp) == 1) {
      if ($tmp[0]->name != '') {
        $smarty->assign('title', $tmp[0]->name);
      }
      if ($tmp[0]->desc != '') {
        $smarty->assign('desc', $tmp[0]->desc);
      }
      if ($tmp[0]->keywords != '') {
        $smarty->assign('keywords', $tmp[0]->keywords);
      }
    }
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $result = DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $content = (count($result->d) == 0) ? '' : $result->d[0]->content;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $opt = new stdClass();
    $opt->select_expr = 't.*, t1.name, t1.folder';
    $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
    $instances = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt);
    
    $scripts = array();
    foreach ($instances->d as &$instance) {
      $script = _script($instance->folder, $instance->id, $instance->width, $instance->height, $instance->scrolling);
      $content = str_replace("{" . $instance->folder . ": " . "$instance->id" . "}", $script, $content);
    }
    
    $smarty->assign('content', $content);
    
    global $menuInst2;
    $result = _getNav($menuInst2, $id);
    if (count($result) >= 1) {
      $id = $result[0]->id;
      if (count($result) == 1) {
        $result = array();
      }
    }
    $smarty->assign('nav', $result);
    
    //TEMPORARY ADDED
    $smarty->assign('idx', $id);
  } else {
    $smarty->assign('content', '');
  }
}

function _script($folder, $instance_id, $width, $height, $scrolling) {
  return "<div>
            <script type=\"text/javascript\">
              function updateHeight() {
                var ifm_$instance_id = document.getElementById(\"plgn_ifm_$instance_id\");
                if (ifm_$instance_id && !window.opera){
                  if (ifm_$instance_id.contentDocument && ifm_$instance_id.contentDocument.body){
                    var boh = ifm_$instance_id.contentDocument.body.offsetHeight;
                    var bsh = ifm_$instance_id.contentDocument.body.scrollHeight;
                    ifm_$instance_id.height = Math.max(boh, bsh) + 20;
                  }
                }
              }
            </script>
            <iframe id=\"plgn_ifm_$instance_id\" onload=\"Javascript:updateHeight()\" width=\"$width\" height=\"$height\"" . (($scrolling == 0) ? " scrolling=\"no\"" : "") . " frameborder=\"0\"  marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"true\" src=\"instance.php?inst_i=$instance_id&inst_f=$folder&inst_s=index&" . $_SERVER['QUERY_STRING'] . "\"></iframe>
          </div>";
}
?>